package com.boot.controller;

import com.boot.entity.Building;
import com.boot.entity.Classroom;
import com.boot.repository.BuildingRepository;
import com.boot.repository.ClassroomRepository;
import com.boot.util.StringUtil;
import com.boot.vo.BaseMessage;
import com.boot.vo.MessageHandler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.util.List;

@RestController
@CrossOrigin(origins = "*")

@RequestMapping("/place")
public class PlaceController {
    @Autowired
    private BuildingRepository buildingRepository;
    @Autowired
    private ClassroomRepository classroomRepository;

    //添加教学楼
    @RequestMapping("addBuilding")
    @ResponseBody
    public BaseMessage<?> addBuilding(@RequestBody Building building) {
        if (StringUtil.isBlank(building.getBuildingName())) {
            return MessageHandler.createFailedVo("建筑名不可为空");
        }
        Boolean isRepeated = buildingRepository.existsByBuildingName(building.getBuildingName());
        if (isRepeated == true)
            return MessageHandler.createFailedVo("该建筑已存在，不可重复添加！");
        else {
            buildingRepository.save(building);
            return MessageHandler.createSuccessVo("建筑添加成功！");
        }
    }

    //获取建筑列表
    @RequestMapping("BuildingList")
    public BaseMessage<?> findALlBuilding() {
        List<Building> buildings = buildingRepository.findAll();
        return MessageHandler.createSuccessVo(buildings, "建筑列表返回成功");
    }


    /**
     * 删除建筑
     */
    @Transactional
    @PostMapping("delBuilding")
    @ResponseBody
    public BaseMessage<?> delBuilding(@RequestParam String buildingName) {
        Building building = buildingRepository.findByBuildingName(buildingName);
        buildingRepository.deleteByBuildingName(buildingName);
        return MessageHandler.createSuccessVo("建筑删除成功！");
    }

    //添加教室
    @RequestMapping("addClassroom")
    @ResponseBody
    public BaseMessage<?> addClassroom(@RequestBody Classroom classroom) {
        if (StringUtil.isBlank(classroom.getClassroomName())) {
            return MessageHandler.createFailedVo("教室不可为空");
        }
        Boolean isRepeated = classroomRepository.existsByClassroomName(classroom.getClassroomName());
        if (isRepeated == true)
            return MessageHandler.createFailedVo("该教室已存在，不可重复添加！");
        else {
            classroomRepository.save(classroom);
            return MessageHandler.createSuccessVo("教室添加成功！");
        }
    }

    //获取教室列表
    @RequestMapping("classroomList")
    @ResponseBody
    public BaseMessage<?> findALlClassroom(@RequestParam Integer buildingId) {
        Building  building=buildingRepository.findByBuildingId(buildingId);
        List<Classroom> classrooms = classroomRepository.findAllByBuildingName(building.getBuildingName());
        return MessageHandler.createSuccessVo(classrooms, "教室列表返回成功");
    }


    /**
     * 删除教室
     */
    @Transactional
    @PostMapping("delClassroom")
    @ResponseBody
    public BaseMessage<?> delClassroom(@RequestParam String classroomName) {
        Classroom classroom = classroomRepository.findByClassroomName(classroomName);
        classroomRepository.deleteByClassroomName(classroomName);
        return MessageHandler.createSuccessVo("教室删除成功！");
    }





}







